/**
 * Created by Aquarius on 2018/5/31.
 */
$(function () {
    // 自定义错误处理
    var error = new Error();
    error.newHandle({
        errorCallback: function (fn) {
            if ($.type(fn) === "function") fn();
        }
    });
    //获取链接地址
    var href = this.location.href;
    var index = href.indexOf("=");
    var id = href.substring(index + 1);
    var selector = {
        title: "#title",
        type: "#type",
        content: "#content",
        file: "#file",
        creatTime: "#creatTime",
        sendTime: "#sendTime",
        announcer: "#announcer",
        attachbox: "#attachbox",
        visTotal: "#visTotal",
        visitors: "#visitors"
    }
    // 接口
    var oDataSource = {
        //查看公告接口
        viewAnnouncement: function (oData) {
            console.log("oDate------", oData)
            return $.ajax({
                url: Site("viewAnnouncement"),
                method: "get",
                data: {data: JSON.stringify(oData)}
            });
        },
        //公告查看与未查看记录
        viewRecord: function (oData) {
            console.log("oDate------", oData)
            return $.ajax({
                url: Site("viewRecord"),
                method: "get",
                data: {data: JSON.stringify(oData)}
            });
        },
    };
    oDataSource.viewRecord({noticeId: Number(id),account:Account.user.account}).then(function (data) {
        error.listen(data.code).not(function () {
            console.log(data)
            $(selector.visTotal).html(data.totalHaveRead);
            if (data.totalHaveRead) {
                data.totalHaveRead = (data.totalHaveRead > 30 ? 30 : data.totalHaveRead)
                for (var i = 0; i < data.totalHaveRead; i++) {
                    var name = data.haveReadName[i].name;
                    var time = data.haveReadName[i].gmtModified
                    time = new Date(time).Format("MM-dd");
                    var vis = "<div class='viss '><span class='vis label title'>" + name + "</span><span class='badge' style='background-color:#eeeeee63;'>" + time + "</span></div>";
                    $(selector.visitors).append(vis);
                }
            }
        });
    });

    oDataSource.viewAnnouncement({noticeId: Number(id), account: Account.user.account}).then(function (data) {
        error.listen(data.code).not(function () {
            attachmentMap = data.attachmentMap
            data = data.announcement
            console.log("title:",data.title)
            $(selector.title).html(data.title); //公告标题
            $(selector.content).html(data.content);//公告内容
            var sendTime = new Date(Number(data.releaseTime)).Format("yyyy-MM-dd")
            var creatTime = new Date(Number(data.gmtCreate)).Format("yyyy年MM月dd日")
            $(selector.sendTime).html(sendTime);
            $(selector.announcer).html(data.name);  //发布者
            $(selector.creatTime).html(creatTime);
            if (attachmentMap) {   //判断是否有附件
                $(selector.attachbox).css({"display": "block"})
                for (var i = 0; i < attachmentMap.length; i++) {
                    $(selector.attachbox).append("<div style='margin:10px'>"+attachmentMap[i].fileName+"&nbsp;&nbsp;&nbsp;<a href='#' value='2' id=" + attachmentMap[i].id + ">预览</a>&nbsp;&nbsp;&nbsp;<a href='javascript:;' value='1' id=" + attachmentMap[i].id + ">下载附件</a></div>")
                }
            }
        })
    });
    $(selector.attachbox).on('click', 'a', function () {
        console.log(this)
        var id = this.id;
        var value = $(this).attr("value");
        var $form = $("<form>");
            $form.attr("style", "display:none");
            $form.attr("action", Site("downAttachment"));
        if(value == 2)
            $form.attr("target", "_blank");
        else
            $form.attr("target", "download");
            $form.attr("method", "get");
        var $input = $("<input>").attr({
            type: "text",
            name: "status",
            value: value
        });
        var $input1 = $("<input>").attr({
            type: "text",
            name: "id",
            value: id
        });
        $form.append($input);
        $form.append($input1);
        $("body").append($form);
        console.log("a.value:",value, $form , $input)
        $form[0].submit();
    });
})